﻿using System;
using System.Text;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Collections.Generic;

using gKodes.Drawing;
using gKodes.Collections;

namespace gKodes.Windows.Forms.Internals
{
    public class HexViewSegment : HexView.PageSegment
    {
        private string FormatString;

        public override string Name
        {
            get { return "Hex"; }
        }

        public override bool IsNavigable
        {
            get { return true; }
        }

        public override bool CanSelect
        {
            get { return true; }
        }

        public override void ResizeTo(Size size)
        {
            base.ResizeTo(size);
            this.FormatString = "X" + this.CellProperties.Size.Width;
        }

        public override void  RenderSegment(Rectangle clip)
        {
            PaintScheme tsHolder;
            Range tbrange;

            long ms = this.OffsetStates.Start;
            foreach (KeyValuePair<Range, int> ofs in this.OffsetStates)
            {
                tbrange = new Range(ofs.Key.Start - ms, ofs.Key.End - ms);
                tsHolder = this.DrawnOn.PaintSchemes[ofs.Value];
                this.FillCell(tbrange, tsHolder.Background);
                this.DrawForeHelper(tbrange, tsHolder.Foreground);
            }
        }

        protected override string HelperSource(int x, int y, long i)
        {
            return this.DrawnOn.Source[(int)i].ToString(
                        this.FormatString, Thread.CurrentThread.CurrentCulture);
        }
    }
}
